package com.dhcc.bpm.common.manager.factory;

import com.dhcc.bpm.common.constant.CommonConstant;
import com.dhcc.bpm.common.utils.*;
import com.dhcc.bpm.modules.log.entity.Logininfor;
import com.dhcc.bpm.modules.log.entity.OperLog;
import com.dhcc.bpm.modules.log.service.ILogininforService;
import com.dhcc.bpm.modules.log.service.IOperLogService;
import com.dhcc.bpm.common.utils.DateUtils;
import com.dhcc.bpm.common.utils.IpUtils;
import com.dhcc.bpm.common.utils.LogUtils;
import com.dhcc.bpm.common.utils.ServletUtils;
import eu.bitwalker.useragentutils.UserAgent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.tool.utils.SpringUtil;

import java.time.LocalDateTime;
import java.util.TimerTask;

/**
 * @ClassName AsyncFactory
 * @Description 异步工厂（产生任务用）
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
public class AsyncFactory {
	private static final Logger sys_user_logger = LoggerFactory.getLogger("sys-user");

	/**
	 * 操作日志记录
	 * 
	 * @param operLog
	 *            操作日志信息
	 * @return 任务task
	 */
	public static TimerTask recordOper(final OperLog operLog) {
		return new TimerTask() {
			@Override
			public void run() {
				// 远程查询操作地点
				operLog.setOperLocation(operLog.getOperIp());
				SpringUtil.getBean(IOperLogService.class).saveOrUpdate(operLog);
			}
		};
	}

	/**
	 * 记录登陆信息
	 * 
	 * @param username
	 *            用户名
	 * @param status
	 *            状态
	 * @param message
	 *            消息
	 * @param args
	 *            列表
	 * @return 任务task
	 */
	public static TimerTask recordLogininfor(final String username, final String status, final String message,
			final Object... args) {
		final UserAgent userAgent = UserAgent.parseUserAgentString(ServletUtils.getRequest().getHeader("User-Agent"));
//		final String ip = "192.168.0.1";
		final String ip = IpUtils.getIpAddr(ServletUtils.getRequest());
		return new TimerTask() {
			@Override
			public void run() {
				StringBuilder s = new StringBuilder();
				s.append(LogUtils.getBlock(ip));
				s.append(ip);
				s.append(LogUtils.getBlock(username));
				s.append(LogUtils.getBlock(status));
				s.append(LogUtils.getBlock(message));
				// 打印信息到日志
				sys_user_logger.info(s.toString(), args);
				// 获取客户端操作系统
				String os = userAgent.getOperatingSystem().getName();
				// 获取客户端浏览器
				String browser = userAgent.getBrowser().getName();
				// 封装对象
				Logininfor logininfor = new Logininfor();
				logininfor.setLoginName(username);
				logininfor.setIpaddr(ip);
				String address = ip;
//				try {
//					address = IpAddressUtils.getAddrName();
//				}catch (IOException e){
//					sys_user_logger.error("获取用户地址错误：{}", e);
//				}
				logininfor.setLoginLocation(address);
				logininfor.setBrowser(browser);
				logininfor.setOs(os);
				logininfor.setMsg(message);
				logininfor.setLoginTime(DateUtils.asDate(LocalDateTime.now()));
				// 日志状态
				if (CommonConstant.LOGIN_SUCCESS.equals(status) || CommonConstant.LOGOUT.equals(status)) {
					logininfor.setStatus(1);
				} else if (CommonConstant.LOGIN_FAIL.equals(status)) {
					logininfor.setStatus(0);
				}
				// 插入数据
				SpringUtil.getBean(ILogininforService.class).saveOrUpdate(logininfor);
			}
		};
	}
}
